package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	pb "src/gRPC/proto"
	"time"
)

/*
1.链接gRPC服务器
2.实例化gRPC客户端
3.组装请求参数
4.调用接口
*/
func main() {

	//1.链接gRPC服务器
	var addr = "127.0.0.1:9099"
	conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		fmt.Println("client connect fail")
	}

	defer conn.Close()

	fmt.Println("client connect ok:", addr)

	//2.实例化gRPC客户端
	var client = pb.NewUserInfoServiceClient(conn)

	//3.组装请求参数
	var req = &pb.UserRequest{
		Name: "zs",
	}

	//4.调用接口
	//添加超时设置
	ctx, _ := context.WithTimeout(context.Background(), time.Second*3)
	resp, err := client.GetUserInfo(ctx, req)

	if err != nil {
		fmt.Println("rpc call fail", err)
	}
	fmt.Printf("%#v \n", resp)

	fmt.Printf("id=%d \n", resp.GetId())
	fmt.Printf("name=%s \n", resp.GetName())
	fmt.Printf("gender=%d \n", resp.GetGender())
	fmt.Printf("hobby=%v \n", resp.GetHobby())

	fmt.Printf("status=%v \n", resp.GetStatus())

	fmt.Printf("phone=%v, %v \n", resp.GetPhone().GetNum(), resp.GetPhone().GetName())

}
